package com.hanbang.gycatering.adpater;

import java.util.ArrayList;
import java.util.List;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.CommentData;
import com.hanbang.gycatering.bean.CommodityData;
import com.hanbang.gycatering.bean.OrderData;
import com.hanbang.gycatering.bean.PropertyBeanData;
import com.hanbang.gycatering.classify.ClassifyList;
import com.hanbang.gycatering.classify.ProductDetail;
import com.hanbang.gycatering.myviews.ScaleImageView;
import com.hanbang.gycatering.utils.BitmapUtil;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.SystemTools;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderAdapter extends BaseAdapter {
	PropertyBeanData entity;
	ViewHolder viewHolder;
	Bitmap bit;
	private ArrayList<OrderData> coll;
	private BitmapUtils bitmapUtils = null;
	private LayoutInflater mInflater;
	private Context context;

	public OrderAdapter(Context context, ArrayList<OrderData> coll) {
		this.coll = coll;
		bitmapUtils = BitmapUtil.getBitmapUtils(context,
				MyApplication.bitmapCachePath);
		mInflater = LayoutInflater.from(context);
		this.context = context;
	}
	public int getCount() {
		return coll.size();
	}

	public Object getItem(int position) {
		return coll.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		MyLog.e("getView", position+"");
		ViewHolder holder = null;
		final OrderData data = coll.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(
					R.layout.item_oder, null);
			holder.order_number = (TextView) convertView
					.findViewById(R.id.order_number);
			holder.twolistview = (ListView) convertView
					.findViewById(R.id.ordertwo_listview);
			holder.signstate = (TextView) convertView
					.findViewById(R.id.order_signstate);
			holder.showinfor = (TextView) convertView
					.findViewById(R.id.show_infor);
			holder.goodstate = (TextView) convertView
					.findViewById(R.id.order_goodstate);
			holder.ll_goodstate = (LinearLayout) convertView
					.findViewById(R.id.ll_goodstate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.showinfor.setText("共"+data.getOdercount()+"件商品"+" "
		+"合计：¥"+data.getTotleprice());
		holder.order_number.setText("订单编号："+data.getOrdernumber());
		switch (data.getOrderstate()) {
		case 5:
			holder.signstate.setText("快件已签收");
			break;
		case 4:
			holder.signstate.setText("快件已签收");
			break;
		case 3:
			holder.signstate.setText("快件未签收");
			break;
		case 2:
			holder.signstate.setText("快件未发出");
			break;
		case 1:
			holder.signstate.setText("待付款");
			break;

		default:
			break;
		}
			holder.twolistview.setAdapter(new OrderAdpaterTwo(context,data.getColl().get(position)));
			holder.twolistview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int posi, long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(context, ProductDetail.class);
					intent.putExtra("id", data.getColl().get(position).get(posi).getId());
					context.startActivity(intent);
				}
			}) ;
			holder.goodstate.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					switch (data.getOrderstate()) {
					case 1:
//						holder.goodstate.setText("确认订单");
						break;
					case 2:
						MyToast.show(context, "已提醒卖家发货", Toast.LENGTH_SHORT);
						arg0.setEnabled(false);
						break;
					case 3:
//						holder.goodstate.setText("确认收货");
						break;
					default:
						break;
					}
				}
			});
			switch (data.getOrderstate()) {
			case 1:
				holder.goodstate.setText("确认订单");
				break;
			case 2:
				holder.goodstate.setText("提醒发货");
				break;
			case 3:
				holder.goodstate.setText("确认收货");
				break;
			case 4:
				holder.ll_goodstate.setVisibility(View.GONE);
				break;
			case 5:
				holder.ll_goodstate.setVisibility(View.GONE);
				break;
			default:
				break;
			}
		
		return convertView;
	}

	private class ViewHolder {
		TextView order_number = null;
		TextView signstate = null;
		TextView goodstate = null;
		TextView showinfor = null;
		ListView twolistview=null;
		private  LinearLayout ll_goodstate;
	}
	class OrderAdpaterTwo extends BaseAdapter {
		PropertyBeanData entity;
		ViewHolder viewHolder;
		Bitmap bit;
		private ArrayList<CommodityData> twolists;
		private LayoutInflater mInflater;
		private Context context;
		private int positions;
		public OrderAdpaterTwo(Context context,ArrayList<CommodityData> arrayList) {
			this.twolists = arrayList;
			mInflater = LayoutInflater.from(context);
			this.context = context;
		}

		public int getCount() {
			if(twolists==null){
				return 0;
			}
			return twolists.size();
		}

		public Object getItem(int position) {
			if (twolists == null) {
				return null;
			}
			return twolists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MyLog.e("getView", position+"");
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(
						R.layout.item_order_two, null);
				holder.odercount = (TextView) convertView
						.findViewById(R.id.oder_count);
				holder.ordertitle = (TextView) convertView
						.findViewById(R.id.order_title);
				holder.oderprice = (TextView) convertView
						.findViewById(R.id.order_price);
				holder.orderimage = (ImageView) convertView
						.findViewById(R.id.itemorder_image);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if(twolists.size()!=0){
				CommodityData data = twolists.get(position);
				bitmapUtils.display(holder.orderimage,SystemTools.getHttpFileUrl(data.getImg()));
				holder.ordertitle.setText(data.getName());
				holder.oderprice.setText("¥"+data.getPrice()+"");
				holder.odercount.setText("×"+data.getCount()+"");
			}
			return convertView;
		}
		private class ViewHolder {
			TextView ordertitle = null;
			TextView oderprice = null;
			TextView odercount = null;
			ImageView orderimage=null;
		}
	}
 

}

